<?php
    if($_REQUEST['B1']){
        include_once($_SERVER['DOCUMENT_ROOT']."/config.php");
        require_once($_SERVER['DOCUMENT_ROOT'] . "/collect/Lib/ORG/Mycurl.class.php");
        include_once($_SERVER['DOCUMENT_ROOT']."/collect/Common/yzs1013_pldr.php");
        $result=urldecode($_REQUEST['str']);
        $s_url=urldecode($_REQUEST['s_url']);
        $re="";
        if($_REQUEST['B1']=="changeurl"){
            if(preg_match("/^http/",$s_url)) {
                $source = curl_file_get_contents($s_url);
                $newurl = createnewurl($result, $s_url, $source, $re);
            }
            $result="\$this->changeurl(\"".str_replace("\"","\\\"",urldecode($newurl))."\");";

        }
        if($_REQUEST['B1']=="nn22"){
            $s=preg_replace("/\n/"," ",$result).":nn22";
            $s=trim(jcre($s));
            $s=preg_replace("/(src|data|parems?|data[^\s]*)\s*=\s*\\\\([\"'])([^<>]*)\\\\\\2/Uisx","\\1=\\\"[^<>]*\\\"",$s);
            $s=preg_replace("/href\s*=\s*\\\\\"([^<>]*)\\\\\"/Uisx","href=\\\"(?<url>[^<>]*)\\\"",$s);
            $s=preg_replace("/href\s*=\s*\\\\\'([^<>]*)\\\\\'/Uisx","href=\\\'(?<url>[^<>]*)\\\'",$s);
            $s=preg_replace("/(<a.*(?<!url|title|\<)>)(.*)(<\\\\\/a>)/Uisx","\\1(?<title>.*)\\3",$s);
            $s=preg_replace("/(title|alt)\s*=\s*\\\\\"([^<>]*)\\\\\"/Uisx","\\1\s*=\s*\\\"(?<title>.*)\\\"",$s);
            $s=preg_replace("/(title|alt)\s*=\s*\\\\\'([^<>]*)\\\\\'/Uisx","\\1\s*=\s*\\\'(?<title>.*)\\\'",$s);
            $s=preg_replace("/(title|alt)\s*=\s*'([^<>]*)'/Uisx","\\1\s*=\s*'(?<title>[^<>]*)'",$s);
            $s=preg_replace("/https?\\\\\:\\\\\/\\\\\/[^<>\"'\s]+(?=\\\\\"|')/isx","[^<>]*",$s);
            $result=$s;
        }

        if($_REQUEST['B1']=="clre"){
            $s=preg_replace("/\n/"," ",$result);
            $s=trim(jcre($s));
            $result=$s;
        }

        if($_REQUEST['u']){
            $result=preg_replace_callback("/([\x{4e00}-\x{9fa5}]+)/uisx",function($em){
                $em[1]=preg_replace_callback("/([\x{4e00}-\x{9fa5}])/uisx",function($em2){
                    $json=json_encode(array("foo"=>$em2[1]));
                    $result=substr($json,8,-2);
                    $result=preg_replace("/\\\u(.*)$/","\x{\\1}",$result);
                    return "$result";
                },$em[1]);
                return $em[1];
            },$result);
        }

        $data['result']=$result;
        $data['re']=$re;

        print_r(json_encode($data));
        exit;
    }



?><!doctype html>
<html lang="zh-cn">
<head>
    <meta charset="UTF-8" http-equiv="X-UA-Compatible" content="IE=Edge">
    <script type="text/javascript" src="/js/jquery-3.2.1.min.js"></script>
    <script type="text/javascript" language="javascript" src="../js/layer/layer.js"></script>
    <title>字串工具</title>
    <style>
        textarea {
            width: 940px;
            height: 98px;
            margin: 10px;
        }
    </style>
    <script>
        $(document).on("click","#B1",function(){
            ajaxTimeoutTest=$.ajax({
                type:'POST',
                async:true,
                timeout : 20000,
                url: "createstr.php?B1="+$("#type").val()+"&u="+$("#u").val()+"&rn="+Math.random(),
                data: {
                    "str": encodeURIComponent($("#str").val()),
                    "s_url": encodeURIComponent($("#s_url",top.document).val()),
                },
                dataType: "json",//html,text,json,jsonp,excel,script
                success: function(data){
                    debugger;
                    $("#result").val(data.result);
                    if(data.re!=""){
                        $("#result").val($("#result").val()+"\n\n在re窗口添加规则:\n"+data.re);
                    }
                }
            });

        })
        function help(o){
            top.layer.open({
                type: 2,
                title: '帮助',
                shadeClose: true,
                shade: 0.8,
                area: ['1000px', '380px'],
                skin: 'layeroverflowhidden',
                scrollbar: false,
                content: ['/collect/reset/help.php?o='+o,'no'] //iframe的url
            });
        }
    </script>
</head>
<body>
<iframe name="ifr" id="ifr" style="display: none"></iframe>
    <form method="POST" name="myform" action="createstr.php">
        <textarea name="str" id="str"></textarea>
        <p algin="center" style="text-align: center">
            <input type="button" id="B1" name="B1" value="<?php echo $_REQUEST['o'];?>转换">
            &nbsp;<input type="checkbox" id="u" name="u" value="1">中文JS化
            <input type="hidden" name="type" id="type" value="<?php echo $_REQUEST['o'];?>">
            <span style="float:right"><a href="javascript:void()"  onclick="help('changeurl')" id="ele_tis">changeurl帮助</a></span>
        </p>
        <textarea name="result" id="result"></textarea>
    </form>
</body>
</html>
